package cn.virens.web.common;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ApplicationObjectSupport;

/**
 * 通用控制器
 * 
 * @文件 :BaseController.java
 * @作者 :cn.loioi
 * @创建时间 :2016年10月13日 下午1:10:27
 */
public class BaseController extends ApplicationObjectSupport {
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	protected String _baseTemplateUrl = "";

	protected BaseController() {
	}

	/**
	 * @param baseTemplateUrl 模板根路径
	 */
	protected BaseController(String baseTemplateUrl) {
		if (!baseTemplateUrl.endsWith("/")) baseTemplateUrl = baseTemplateUrl + "/";
		this._baseTemplateUrl = baseTemplateUrl;
	}

	/**
	 * 返回模板地址
	 * 
	 * @param url 模板路径
	 * @return
	 */
	protected final String returnUrl(String url) {
		if (StringUtils.isEmpty(url)) {
			return _baseTemplateUrl + "index";
		} else if (url.startsWith("/")) {
			return _baseTemplateUrl + url.substring(1);
		} else {
			return _baseTemplateUrl + url;
		}

	}

	/**
	 * 页面重定向
	 * 
	 * @param url
	 * @return
	 */
	protected final String redirectUrl(String url) {
		if (url.lastIndexOf(".jspx") != -1) {
			return "redirect:" + url;
		} else {
			return "redirect:" + url + ".jspx";
		}
	}

	/**
	 * 请求转发
	 * 
	 * @param url
	 * @return
	 */
	protected final String forwardUrl(String url) {
		return "forward:" + url;
	}

	/**
	 * 获取Session
	 * 
	 * @return
	 */
	protected final Session getSession(HttpServletRequest request) {
		return getSession(request, true);
	}

	/**
	 * 获取Session
	 * 
	 * @param create 是否自动创建session
	 * @return
	 */
	protected final Session getSession(HttpServletRequest request, boolean create) {
		return getSubject(request).getSession(create);
	}

	/**
	 * 获取Subject
	 * 
	 * @return
	 */
	protected final Subject getSubject(HttpServletRequest request) {
		return SecurityUtils.getSubject();
	}

	/**
	 * 检查权限
	 * 
	 * @param permission
	 */
	protected final void checkPermission(HttpServletRequest request, String... permission) {
		getSubject(request).checkPermissions(permission);
	}

	/**
	 * 检查角色
	 * 
	 * @param roleIdentifiers
	 */
	protected final void checkRoles(HttpServletRequest request, String... roleIdentifiers) {
		getSubject(request).checkRoles(roleIdentifiers);
	}

	/**
	 * 获取session值
	 * 
	 * @param name session键
	 * @return
	 */
	protected final Object getAttribute(HttpServletRequest request, String name) {
		Session session = getSession(request, false);
		if (session != null) {
			return session.getAttribute(name);
		} else {
			return null;
		}
	}

	/**
	 * 获取session值
	 * 
	 * @param name session键
	 * @param clazz session键类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected final <T> T getAttribute(HttpServletRequest request, String name, Class<T> clazz) {
		Object obj = getAttribute(request, name);
		if (obj != null && obj.getClass().isAssignableFrom(clazz)) {
			return (T) obj;
		} else {
			return null;
		}
	}

	/**
	 * 存入session值
	 * 
	 * @param name session键
	 * @param value session值
	 * @return
	 */
	protected final Object setAttribute(HttpServletRequest request, String name, Object value) {
		Session session = getSession(request);
		if (session != null) {
			session.setAttribute(name, value);

			return value;
		} else {
			return null;
		}
	}

}
